//
//  CourseDetailViewController.m
//  Codingke2016
//
//  Created by qianfeng on 16/5/31.
//  Copyright © 2016年 xujiangtao. All rights reserved.
//

#import "CourseDetailViewController.h"

@interface CourseDetailViewController ()<UIScrollViewDelegate>
{
    UIView *_headView;
    UILabel *_courseContents;// 课程目录
    UILabel *_courseDetail; //课程详情
    UILabel *_relativeCourse;//相关课程
    UIView *_greenLine; //绿色的线
    UIView *_lineView;
    UIScrollView *_mainScrollView;
    
}
@end

@implementation CourseDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self makeNav];
    [self initData];
    [self makeHeadView];
    [self makeMainScrollView];
    
}
-(void)initData{
    
}
-(void)makeMainScrollView{
    
    CGFloat bodyHeight = SCREEN_HEIGHT - 342/2 - NAV_HEIGHT;
    
    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 342/2+NAV_HEIGHT, SCREEN_WIDTH, bodyHeight)];
    [_mainScrollView setPagingEnabled:YES];
    [_mainScrollView setShowsHorizontalScrollIndicator:NO];
    [_mainScrollView setBackgroundColor:[UIColor whiteColor]];
    [_mainScrollView setDelegate:self];
    _mainScrollView.bounces = NO;
    [_mainScrollView setContentSize:CGSizeMake(SCREEN_WIDTH*3,bodyHeight)];
    [self.view addSubview:_mainScrollView];

}
-(void)makeNav{
    self.title =@"课程详情";
    self.view.backgroundColor =[UIColor whiteColor];
}
-(void)makeHeadView{

    _headView =[[UIView alloc] initWithFrame:CGRectMake(0,NAV_HEIGHT, SCREEN_WIDTH,260/2)];
    _headView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:_headView];
    
    UIView *grayView =[[UIView alloc] initWithFrame:CGRectMake(0,230/2, SCREEN_WIDTH,15)];
    grayView.backgroundColor =[UIColor colorWithHexString:@"#eaedf0"];
    [_headView addSubview:grayView];
    
    
    UIView *middleView =[UIView new];
    middleView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:middleView];
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(grayView.mas_bottom);
        make.width.equalTo(grayView);
        make.height.mas_equalTo(40);
    }];
    
    _lineView =[UIView new];
    _lineView.backgroundColor =[UIColor colorWithHexString:@"#c0bfbf"];
    [middleView addSubview:_lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.width.equalTo(middleView);
        make.bottom.equalTo(middleView).offset(-1);
    }];
    
    _courseDetail = [UILabel new];
    _courseDetail.textColor = [UIColor colorWithHexString:@"#949494"];
    _courseDetail.text =@"课程介绍";
    _courseDetail.userInteractionEnabled = YES;
    [middleView addSubview:_courseDetail];
    _courseDetail.font =[UIFont systemFontOfSize:17];
    [_courseDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(middleView);
    }];
    
    NSInteger space = SCREEN_WIDTH*50/375;
    
    _courseContents =[UILabel new];
    _courseContents.textColor =MAINGREENCOLOR;
    [self.view addSubview:_courseContents];
    _courseContents.text =@"课程目录";
    _courseContents.userInteractionEnabled=YES;
    _courseContents.font =[UIFont systemFontOfSize:17];
    [_courseContents mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(middleView);
        make.right.equalTo(_courseDetail.mas_left).offset(-space);
    }];
    
    _relativeCourse =[UILabel new];
    _relativeCourse.textColor =[UIColor colorWithHexString:@"#949494"];
    [self.view addSubview:_relativeCourse];
    _relativeCourse.text =@"相关课程";
    _relativeCourse.userInteractionEnabled=YES;
    _relativeCourse.font =[UIFont systemFontOfSize:17];
    [_relativeCourse mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(middleView);
        make.left.equalTo(_courseDetail.mas_right).offset(space);
    }];
    
    _greenLine =[UIView new];
    _greenLine.backgroundColor =MAINGREENCOLOR;
    [middleView addSubview:_greenLine];
    [_greenLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_courseContents);
        make.bottom.equalTo(_lineView).offset(-0.5);
        make.width.equalTo(_courseContents.mas_width).offset(20);
        make.height.mas_equalTo(2);
    }];
    
    UITapGestureRecognizer *tapGesContent =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesLabel:)];
    [_courseContents addGestureRecognizer:tapGesContent];
    
    UITapGestureRecognizer *tapGesDetail =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesLabel:)];
    [_courseDetail addGestureRecognizer:tapGesDetail];
    
    UITapGestureRecognizer *tapGesRelative =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesLabel:)];
    [_relativeCourse addGestureRecognizer:tapGesRelative];
    
}
-(void)tapGesLabel:(UITapGestureRecognizer*)tapGes{

    if([tapGes.view isEqual:_courseContents]){
        _mainScrollView.contentOffset = CGPointMake(0, 0);
        [self courseContentChnage];
        
    }else if ([tapGes.view isEqual:_courseDetail]){
        _mainScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        [self courseDetailChange];
    
    }else{
        _mainScrollView.contentOffset = CGPointMake(SCREEN_WIDTH*2, 0);
        [self reletiveCourseChange];
    }
}
-(void)courseContentChnage{
    _courseContents.textColor = MAINGREENCOLOR;
    _courseDetail.textColor = [UIColor colorWithHexString:@"#949494"];
    _relativeCourse.textColor = [UIColor colorWithHexString:@"#949494"];
    [_greenLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_courseContents);
    }];
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        [self.view layoutIfNeeded];
        
    } completion:nil];

}
-(void)courseDetailChange{
    NSInteger space = SCREEN_WIDTH*50/375;
    UILabel *contentLanel = [UILabel new];
    contentLanel.text =@"课程详情";
    contentLanel.font =[UIFont systemFontOfSize:17];
    [contentLanel sizeToFit];
    NSInteger labelWidth = contentLanel.frame.size.width;
    _courseDetail.textColor = MAINGREENCOLOR;
    _courseContents.textColor = [UIColor colorWithHexString:@"#949494"];
    _relativeCourse.textColor = [UIColor colorWithHexString:@"#949494"];
    
    [_greenLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_courseContents).offset(space+labelWidth);
    }];
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        [self.view layoutIfNeeded];
        
    } completion:nil];

}
-(void)reletiveCourseChange{
    NSInteger space = SCREEN_WIDTH*50/375;
    UILabel *contentLanel = [UILabel new];
    contentLanel.text =@"课程详情";
    contentLanel.font =[UIFont systemFontOfSize:17];
    [contentLanel sizeToFit];
    NSInteger labelWidth = contentLanel.frame.size.width;
    _relativeCourse.textColor = MAINGREENCOLOR;
    _courseDetail.textColor = [UIColor colorWithHexString:@"#949494"];
    _courseContents.textColor = [UIColor colorWithHexString:@"#949494"];
    [_greenLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_courseContents).offset(space*2+labelWidth*2);
    }];
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        [self.view layoutIfNeeded];
    } completion:nil];

}
#pragma mark - ScrollView Delegte
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == _mainScrollView) {
        int index = scrollView.contentOffset.x / SCREEN_WIDTH;
        if(index==0){
           
            [self courseContentChnage];
            
        }else if(index ==1){
            [self courseDetailChange];
            
        }else{
            [self reletiveCourseChange];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
